package cn.fyl.org.service.impl;

import cn.fyl.basic.exception.BusinessException;
import cn.fyl.basic.service.impl.BaseServiceImpl;
import cn.fyl.basic.util.BaiduAuditUtils;
import cn.fyl.org.domain.Employee;
import cn.fyl.org.domain.Shop;
import cn.fyl.org.domain.ShopLog;
import cn.fyl.org.dto.ShopDto;
import cn.fyl.org.mapper.EmployeeMapper;
import cn.fyl.org.mapper.ShopLogMapper;
import cn.fyl.org.mapper.ShopMapper;
import cn.fyl.org.service.IEmployeeService;
import cn.fyl.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

@Service
//查询的方法比较多，使用查询的事务配置
//只读，事务传播机制=SUPPORTS，有我就用，没有事务就不用
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopLogMapper shopLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public void settlement(Shop shop) {
        // 1.空值检验
        if(StringUtils.isEmpty(shop.getName()) ||
            StringUtils.isEmpty(shop.getTel()) ||
            StringUtils.isEmpty(shop.getAddress()) ||

            StringUtils.isEmpty(shop.getAdmin().getUsername()) ||
            StringUtils.isEmpty(shop.getAdmin().getEmail()) ||
            StringUtils.isEmpty(shop.getAdmin().getPhone()) ||
            StringUtils.isEmpty(shop.getAdmin().getPassword()) ||
            StringUtils.isEmpty(shop.getAdmin().getComfirmPassword())
        ){
            throw new BusinessException("信息不能为空");
        }

        Boolean aBoolean = BaiduAuditUtils.TextCensor(shop.getName());
        if(!aBoolean) { // 名称有问题
            throw new BusinessException("店铺名称违规！");
        }

        Employee admin = shop.getAdmin();

        // 2.两次输入的密码判断
        if(!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BusinessException("两次密码不一致，请重新输入");
        }
        // 3.检验是否已经注册过
        Shop shopMsg = shopMapper.loadByNameAndAddress(shop);
        if(shopMsg !=null){
            throw new BusinessException("店铺已经入驻，请直接登陆");
        }
        // 入驻业务
        // 将获取的emp的自增id赋值给shop
        employeeMapper.save(admin);
        // 将emp的自增id赋值给shop的admin_id
        shop.setAdmin_id(admin.getId());

        // 在shop里面添加一个数据
        shopMapper.save(shop);
        // 将shop的id赋值给emp的shop_id
        admin.setShop_id(shop.getId());
        // 更新数据库
        employeeMapper.update(admin);



    }

    /**
     * 审核通过
     * @param log
     */

    @Override
    public void pass(ShopLog log) throws MessagingException {
        // 1.修改状态
        Long shop_id = log.getShop_id();
        Shop shop = shopMapper.loadById(shop_id);
        shop.setState(2);
        shopMapper.update(shop);
        // 2.保存数据到日志表
        log.setAudit_id(1L);
        log.setState(2);
        shopLogMapper.save(log);
        // 3.发送邮件
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("2099227475@qq.com");
        helper.setSubject("店铺激活邮件");
        helper.setText("<h1>你的店铺已经注册!!!</h1><a href='http://localhost:8080/shop/active/"+log.getShop_id()+"'>点击该链接激活</a>",true);

        //获取收件人
        //String email = shop.getAdmin().getEmail();

        // 发送邮件
        helper.setTo("2099227475@qq.com");
        javaMailSender.send(mimeMessage);

    }

    /**
     * 驳回
     * @param log
     */
    @Override
    public void reject(ShopLog log) throws MessagingException {
        // 1.修改状态
        Long shop_id = log.getShop_id();
        Shop shop = shopMapper.loadById(shop_id);
        shop.setState(4);
        shopMapper.update(shop);
        // 2.保存数据到日志表
        log.setAudit_id(1L);
        log.setState(4);
        shopLogMapper.save(log);
        // 3.发送邮件
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("2099227475@qq.com");
        helper.setSubject("店铺驳回邮件");
        helper.setText("<h1>你的店铺已被驳回!!!</h1><a href='http://localhost:8080/shop/active/"+log.getShop_id()+"'>点击该链接激活</a>",true);

        //获取收件人
        //String email = shop.getAdmin().getEmail();

        helper.setTo("2099227475@qq.com");
        javaMailSender.send(mimeMessage);

    }

    @Override
    public List<ShopDto> getEcharts() {
        return shopMapper.getEcharts();
    }
}
